package com.material.vtool.utils;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class AliImgUtil {

    // Endpoint以杭州为例，其它Region请按实际情况填写。
    private static final String endpoint = "http://oss-cn-shenzhen.aliyuncs.com";

    // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
    private static final String accessKeyId = "LTAI4Fh7PnYLw3uQvoUYiazS";

    private static final String accessKeySecret = "cEw7e8QMz0h4XY4qmzCoTzhHITqyUU";

    // bucket名字
    private static final String bucketName = "zktuistatic";

    /**
     * 上传微信公众号文章图片
     * @param userId
     * @param imgUrl
     * @param imgName
     */
    public static void uploadWechatArticleImg(String userId, String imgUrl, String imgName) {

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 上传网络流。
        InputStream inputStream = null;
        try {
            inputStream = new URL(imgUrl).openStream();

            ossClient.putObject(bucketName, SysConstant.WX_PN_ARTICLE_IMG_FOLDER_PATH + userId +"/" + imgName, inputStream);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        // 关闭OSSClient。
        ossClient.shutdown();
    }

    /**
     * 上传爬虫文章图片
     * @param articleId
     * @param imgUrl
     * @param imgName
     */
    public static void uploadSpiderArticleImg(Integer articleId, String imgUrl, String imgName) {

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 上传网络流。
        InputStream inputStream = null;
        try {
            inputStream = new URL(imgUrl).openStream();

            ossClient.putObject(bucketName, SysConstant.PLATFORM_ARTICLE_IMG_FOLDER_PATH + articleId +"/" + imgName, inputStream);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        // 关闭OSSClient。
        ossClient.shutdown();
    }

    /**
     * 上传爬虫文章音频
     * @param articleId
     * @param audioUrl
     * @param imgName
     */
    public static void uploadSpiderArticleAudio(Integer articleId, String audioUrl, String imgName) {

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 上传网络流。
        InputStream inputStream = null;
        try {
            inputStream = new URL(audioUrl).openStream();

            ossClient.putObject(bucketName, SysConstant.PLATFORM_ARTICLE_AUDIO_FOLDER_PATH + articleId +"/" + imgName, inputStream);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        // 关闭OSSClient。
        ossClient.shutdown();
    }

    /**
     * 上传爬虫文章缩略图
     * @param articleId
     * @param thumbnailUrl
     * @param imgName
     */
    public static void uploadSpiderArticleThumbnail(Integer articleId, String thumbnailUrl, String imgName) {

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 上传网络流。
        InputStream inputStream = null;
        try {
            inputStream = new URL(thumbnailUrl).openStream();

            ossClient.putObject(bucketName, SysConstant.PLATFORM_ARTICLE_THUMBNAIL_FOLDER_PATH + articleId +"/" + imgName, inputStream);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        // 关闭OSSClient。
        ossClient.shutdown();
    }


    /**
     * 上传微信二维码图片
     * @param userId
     * @param imgName
     * @param qrCodeFile
     */
    public static void uploadWechatQrCodeImg(String userId, String imgName, File qrCodeFile) {

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        ossClient.putObject(bucketName, SysConstant.WX_PN_QRCODE_IMG_FOLDER_PATH+ userId +"/" + imgName, qrCodeFile);

        // 关闭OSSClient。
        ossClient.shutdown();
    }

    public static void main(String[] args) {
        uploadWechatArticleImg("1000001", "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2171717409,2602158110&fm=27&gp=0.jpg", "test.jpg");
    }
}
